package zbase;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;

public class FindDialog extends javax.swing.JDialog {

    private Integer FoundZ = null;

    public Integer getFoundZ() {
        return FoundZ;
    }

    public FindDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setMinimumSize(this.getSize());
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Поиск заявок");

        jButton1.setText("Поиск");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Текст или номер:");

        jButton2.setText("Закрыть");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        jLabel2.setText("Результаты поиска:");

        jButton3.setText("Переход");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 369, Short.MAX_VALUE)
                        .addComponent(jButton2))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 561, Short.MAX_VALUE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 561, Short.MAX_VALUE)
                    .addComponent(jLabel2)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (jTextField1.getText().length() == 0) {
            return;
        }

        Integer zn = null;
        try {
            zn = Integer.parseInt(jTextField1.getText());
        } catch (NumberFormatException numberFormatException) {
        }

        DB db = new DB();
        ResultSet rs = null;
        if (zn != null) {
            rs = db.getResultSet("SELECT zmain.k, zmain.zdatetime, objects.objname, zmain.ztext, services.servicename, zmain.zprim, zmain.zalert, zmain.zdone, zmain.zdonedt, zmain.hide FROM zmain LEFT OUTER JOIN services ON zmain.zservice = services.k LEFT OUTER JOIN objects ON zmain.zobject = objects.k WHERE zmain.hide = 0 AND zmain.k = " + zn.toString());
        } else {
            rs = db.getResultSet("SELECT zmain.k, zmain.zdatetime, objects.objname, zmain.ztext, services.servicename, zmain.zprim, zmain.zalert, zmain.zdone, zmain.zdonedt, zmain.hide FROM zmain LEFT OUTER JOIN services ON zmain.zservice = services.k LEFT OUTER JOIN objects ON zmain.zobject = objects.k WHERE zmain.hide = 0 AND (zmain.ztext LIKE '%" + Vars.getHexString(jTextField1.getText()) + "%' OR zmain.zprim LIKE '%" + Vars.getHexString(jTextField1.getText()) + "%')");
        }
        DefaultListModel lm = new DefaultListModel();
        try {
            while (rs.next()) {
                Calendar zdatetime = new GregorianCalendar();
                zdatetime.setTimeInMillis(rs.getTimestamp("zmain.zdatetime").getTime());
                lm.addElement(
                        new FindObject(
                        rs.getInt("zmain.k"),
                        zdatetime,
                        Vars.getStringFromHex(rs.getString("objects.objname")),
                        Vars.getStringFromHex(rs.getString("zmain.ztext")),
                        Vars.getStringFromHex(rs.getString("services.servicename")),
                        Vars.getStringFromHex(rs.getString("zmain.zprim"))));
            }
        } catch (SQLException ex) {
            Logger.getLogger(FindDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
        db.closeResultSet();
        jList1.setModel(lm);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
        if (jList1.getSelectedIndex() != -1 && evt.getClickCount() > 1) {
            FindObject fo = (FindObject) jList1.getSelectedValue();
            FoundZ = fo.k;
            dispose();
        }
    }//GEN-LAST:event_jList1MouseClicked

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        if (jList1.getSelectedIndex() != -1) {
            FindObject fo = (FindObject) jList1.getSelectedValue();
            FoundZ = fo.k;
            dispose();
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private class FindObject {

        Integer k;
        Calendar zdt;
        String obj;
        String ztxt;
        String srv;
        String prim;

        public FindObject(Integer k, Calendar zdt, String obj, String ztxt, String srv, String prim) {
            this.k = k;
            this.zdt = zdt;
            this.obj = obj;
            this.ztxt = ztxt.replaceAll("\n", " ");
            this.srv = srv;
            this.prim = prim.replaceAll("\n", " ");
        }

        @Override
        public String toString() {
            SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
            return k.toString() + " / " + sdf.format(zdt.getTime()) + " / " + obj + " / " + srv + " / " + ztxt + " / " + prim;
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
